package com.turing.post.cardmanagement.impl;

import java.math.BigInteger;
import java.util.Date;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.cardmanagement.util.MakeCardApproval;
import com.turing.post.cardmanagement.util.MakeCardApprovalDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;

/**
 * @author ys
 * @Dec 18, 2012
 */
@Stateless(mappedName = "cardmanagement.impl.MakeCardApprovalDaoImpl")
@Remote
@Local
public class MakeCardApprovalDaoImpl implements MakeCardApprovalDao {

	/**
	 * 实体管理器
	 */
	@PersistenceContext
	protected EntityManager _em;

	/**
	 * 根据发卡方机构号，查询到下属所有单位的申请制卡信息
	 */
	// @SuppressWarnings("unchecked")
	// @Override
	// public List<MakeCardApproval> getAllMakeCardApprovalByIssueBranchNo(
	// String IssueBranchNo) {
	// String sql = "select * from makecardapproval where issuebranchno like
	// rtrim('"
	// + IssueBranchNo + "','0')||'%'";
	// Query query = _em.createNativeQuery(sql, MakeCardApproval.class);
	// return query.getResultList();
	// }
	/**
	 * 根据发卡方机构号，分状态查询到下属所有单位的申请制卡信息
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<MakeCardApproval> getAllMakeCardApprovalByIssueBranchNoAndStatus(
			String IssueBranchNo, String Status) {

		String sql = "select * from makecardapproval where issuebranchno like rtrim('"
				+ IssueBranchNo + "','0')||'%' and status='" + Status + "'";
		Query query = _em.createNativeQuery(sql, MakeCardApproval.class);
		return query.getResultList();
	}

	/**
	 * 根据发卡方机构号，查询到本单位所有的申请制卡信息
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<MakeCardApproval> getMakeCardApprovalByIssueBranchNo(
			String IssueBranchNo) {

		String sql = "select * from makecardapproval where issuebranchno ='"
				+ IssueBranchNo + "'";
		Query query = _em.createNativeQuery(sql, MakeCardApproval.class);
		return query.getResultList();
	}

	/**
	 * 根据发卡方机构号，分状态查询到本单位所有的申请制卡信息
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<MakeCardApproval> getMakeCardApprovalByIssueBranchNoAndStatus(
			String IssueBranchNo, String Status) {

		String sql = "select * from makecardapproval where issuebranchno ='"
				+ IssueBranchNo + "' and status='" + Status + "'";
		Query query = _em.createNativeQuery(sql, MakeCardApproval.class);
		return query.getResultList();
	}

	/**
	 * 查询单个信息
	 */
	@Override
	public MakeCardApproval getMakeCardApprovalByMakeCardApplyForId(
			BigInteger MakeCardApplyForId) {
		return _em.find(MakeCardApproval.class, MakeCardApplyForId);
	}

	/**
	 * 锁表用作修改
	 */
	@Override
	public MakeCardApproval getMakeCardApprovalByMakeCardApplyForIdForUpdate(
			BigInteger MakeCardApplyForId) {
		String sql = "select * from makecardapproval where MakeCardApplyForId ='"
				+ MakeCardApplyForId + "' for update";
		Query query = _em.createNativeQuery(sql, MakeCardApproval.class);
		return (MakeCardApproval) query.getSingleResult();
	}

	/**
	 * 根据操作员号码取得该操作员发起的制卡申请
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<MakeCardApproval> getMakeCardApprovalByOperNo(BigInteger OperNo) {

		String sql = "select * from makecardapproval where OperId='" + OperNo
				+ "'";
		Query query = _em.createNativeQuery(sql, MakeCardApproval.class);
		return query.getResultList();
	}

	/**
	 * 根据操作员号码分状态取得该操作员发起的制卡申请
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<MakeCardApproval> getMakeCardApprovalByOperNoAndStatus(
			BigInteger OperNo, String Status) {

		String sql = "select * from makecardapproval where OperId='" + OperNo
				+ "' and status='" + Status + "'";
		Query query = _em.createNativeQuery(sql, MakeCardApproval.class);
		return query.getResultList();
	}

	/**
	 * 根据审批操作员号码取得该操作员审批的制卡申请
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<MakeCardApproval> getMakeCardApprovalByAuthOperNo(
			BigInteger AuthOperNo) {

		String sql = "select * from makecardapproval where OperId='"
				+ AuthOperNo + "'";
		Query query = _em.createNativeQuery(sql, MakeCardApproval.class);
		return query.getResultList();
	}

	/**
	 * 根据拒绝操作员号码取得该操作员拒绝的制卡申请
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<MakeCardApproval> getMakeCardApprovalByRejectedOperNo(
			BigInteger RejectedOperNo) {

		String sql = "select * from makecardapproval where rejectedoperno='"
				+ RejectedOperNo + "'";
		Query query = _em.createNativeQuery(sql, MakeCardApproval.class);
		return query.getResultList();
	}

	@Override
	public void insertMakeCardApproval(MakeCardApproval mca)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, mca);

	}

	@Override
	public void updateMakeCardApproval(MakeCardApproval mca)
			throws PCPSApplicationException {
		GeneralDao.update(_em, mca);
	}

	/**
	 * 根据登录发卡商,是否全局，时间段，制卡商ID,卡种类ID,卡片面值,状态 组合条件分页查询数据.
	 * 
	 * @param issuecompanyno
	 *            发卡商ID
	 * @param flag
	 *            是否全局，-1查询全局，1查询本机构
	 * @param issuebranchno
	 *            发卡商机构编号
	 * @param starttime
	 *            开始时间 1900-01-01 查全部
	 * @param endtime
	 *            结束时间 1900-01-01 查全部
	 * @param cardManufactureId
	 *            制卡商ID-1 查全部
	 * @param status
	 *            状态-1查全部
	 * @param page
	 *            页码 -1查全部
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<MakeCardApproval> getMakeCardApprovalByConditions(
			String issuecompanyno, Integer flag, String issuebranchno,
			Date starttime, Date endtime, BigInteger cardManufactureId,
			BigInteger cardFaceId, String status, Integer page) {
		String sql = "select t.* from makecardapproval t where t.issuecompanyno=?1 "
				+ "and (-1=?2 or t.issuebranchno=?3) "
				+ "and (-1<>?2 or t.issuebranchno in (select subbranchno from SYSBRANCHSUBORD where branchno=?3)) "
				+ "and t.applyfortime>=to_date(?4,'yyyy-mm-dd hh24:mi:ss') "
				+ "and t.applyfortime<=to_date(?5,'yyyy-mm-dd hh24:mi:ss') "
				+ "and (-1=?6 or t.cardmanufactureid=?6) "
				+ "and (-1=?7 or t.cardFaceId=?7) "
				+ "and ('-1'=?8 or t.status=?8) order by ApplyForTime desc";
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		Query query = _em.createNativeQuery(sql, MakeCardApproval.class);
		query.setParameter(1, issuecompanyno);
		query.setParameter(2, flag);
		query.setParameter(3, issuebranchno);
		query.setParameter(4, PCPSUtil.getStringYMDByDate(starttime)
				+ " 00:00:00");
		query.setParameter(5, PCPSUtil.getStringYMDByDate(endtime)
				+ " 23:59:59");
		query.setParameter(6, cardManufactureId);
		query.setParameter(7, cardFaceId);
		query.setParameter(8, status);
		if (page != -1) {
			query.setFirstResult(startPosition);
			query.setMaxResults(maxResult);
		}
		return query.getResultList();
	}

	/**
	 * 根据登录用户ID,制卡商ID,卡种类ID,卡片面值,状态 查询总数
	 */
	@Override
	public Integer getMakeCardApprovalTotalByConditions(String issuecompanyno,
			Integer flag, String issuebranchno, Date starttime, Date endtime,
			BigInteger cardManufactureId, BigInteger cardFaceId, String status) {
		String sql = "select nvl(count(*),0) from makecardapproval t where t.issuecompanyno=?1 "
				+ "and (-1=?2 or t.issuebranchno=?3) "
				+ "and (1=?2 or t.issuebranchno in (select subbranchno from SYSBRANCHSUBORD where branchno=?3)) "
				+ "and (?4 is null or t.applyfortime>=to_date(?4,'yyyy-mm-dd hh24:mi:ss'))"
				+ "and (?5 is null or t.applyfortime<=to_date(?5,'yyyy-mm-dd hh24:mi:ss')) "
				+ "and (-1=?6 or t.cardmanufactureid=?6) "
				+ "and (-1=?7 or t.cardFaceId=?7) "
				+ "and ('-1'=?8 or t.status=?8)";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, issuecompanyno);
		query.setParameter(2, flag);
		query.setParameter(3, issuebranchno);
		if (starttime == null) {
			query.setParameter(4, "");
		} else {
			query.setParameter(4, PCPSUtil.getStringYMDByDate(starttime)
					+ " 00:00:00");
		}
		if (endtime == null) {
			query.setParameter(5, "");
		} else {
			query.setParameter(5, PCPSUtil.getStringYMDByDate(endtime)
					+ " 23:59:59");
		}
		query.setParameter(6, cardManufactureId);
		query.setParameter(7, cardFaceId);
		query.setParameter(8, status);
		return Integer.parseInt(query.getSingleResult().toString());
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MakeCardApproval> getApprovalMakeCardByConditions(
			BigInteger OperId, String IssueBranchNo,
			BigInteger cardManufactureId, BigInteger cardFaceId, String status,
			Integer page) {
		if (page == null || page < 0) {
			page = 0;
		}
		String sqlwhere = "";
		if (null != OperId) {
			// sqlwhere+=" and t.OperId!="+OperId;
		}
		if (null != IssueBranchNo) {
			sqlwhere += " and t.issuebranchno like rtrim('" + IssueBranchNo
					+ "','0')||'%' ";
		}
		if (null != cardManufactureId) {
			sqlwhere += " and t.cardmanufactureid=" + cardManufactureId;
		}
		if (null != cardFaceId) {
			sqlwhere += " and t.cardFaceId=" + cardFaceId;
		}
		if (!PCPSUtil.isEmpty(status)) {
			sqlwhere += " and t.status=" + status;
		}
		sqlwhere += " order by t.ApplyForTime desc";
		String sql = "select t.* from makecardapproval t where 1=1 " + sqlwhere;
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		Query query = _em.createNativeQuery(sql, MakeCardApproval.class);
		query.setFirstResult(startPosition);
		query.setMaxResults(maxResult);
		return query.getResultList();
	}

	@Override
	public Integer getApprovalMakeCardTotalByConditions(BigInteger OperId,
			String IssueBranchNo, BigInteger cardManufactureId,
			BigInteger cardFaceId, String status) {
		String sqlwhere = "";
		if (null != OperId) {
			// sqlwhere+=" and t.OperId!="+OperId;
		}
		if (null != IssueBranchNo) {
			sqlwhere += " and t.issuebranchno like rtrim('" + IssueBranchNo
					+ "','0')||'%' ";
		}
		if (null != cardManufactureId) {
			sqlwhere += " and t.cardmanufactureid=" + cardManufactureId;
		}
		if (null != cardFaceId) {
			sqlwhere += " and t.cardFaceId=" + cardFaceId;
		}
		if (!PCPSUtil.isEmpty(status)) {
			sqlwhere += " and t.status=" + status;
		}
		String sql = "select nvl(count(*),0) from makecardapproval t where 1=1 "
				+ sqlwhere;
		Query query = _em.createNativeQuery(sql);
		return Integer.parseInt(query.getSingleResult().toString());
	}
	
	/**
	 * 根据申请单号删除制卡申请
	 */
	@Override
	public void delete(MakeCardApproval mac) throws PCPSApplicationException {
		GeneralDao.remove(_em, mac);
	}
}
